package com.heima.article.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.AuthorService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @作者 itcast
 * @创建日期 2021/11/18 15:27
 **/
@RestController
@RequestMapping("api/v1/author")
public class ApAuthorController {
    @Autowired
    AuthorService authorService;

    @Autowired
    ApArticleService apArticleService;

    @GetMapping("findByUserId/{id}")
    public ResponseResult findByUser(@PathVariable("id") Integer userId) {
        ApAuthor one = authorService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId, userId));
        return ResponseResult.okResult(one);
    }

    @PostMapping("save")
    public ResponseResult save(@RequestBody ApAuthor apAuthor) {
        authorService.save(apAuthor);
        return ResponseResult.okResult();
    }

    /**
     * @功能: 通过自媒体用户ID查询作者Id后查出文章ID
     * @return: com.heima.model.common.dtos.ResponseResult
     */
    @GetMapping("findByVmUserId/{wmUserId}")
    public ResponseResult findByVmUserId(@PathVariable("wmUserId") Integer wmUserId) {
        ApAuthor apAuthor = authorService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getWmUserId, wmUserId));
        List<Long> articleIds = apArticleService.findByAuthorId(apAuthor.getId());
        return ResponseResult.okResult(articleIds);
    }
}
